package com.u3k.app.mid.core;

import android.content.Context;
import android.text.TextUtils;
import com.u3k.app.mid.utils.statistics.StatisticsManager;
import com.u3k.mid.LogUtils;
import com.u3k.mid.ModuleInfo;
import com.u3k.mid.ae;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map.Entry;

public class ModuleApiManager {
    private static final String TAG = "ModuleApiManager";
    static ModuleApiManager mInstance;
    private Context mContext;

    public ModuleApiManager(Context context) {
        this.mContext = context.getApplicationContext();
    }

    private boolean checkParameterType(Method method, Object[] objArr) {
        Class[] parameterTypes = method.getParameterTypes();
        LogUtils.a(TAG, "checkParameterType()  method=" + method.getName() + ", parameters=" + Arrays.toString(objArr) + ", types=" + Arrays.toString(parameterTypes));
        if (parameterTypes.length != objArr.length) {
            return false;
        }
        for (int i = 0; i < parameterTypes.length; i++) {
            if (objArr[i] == null) {
                LogUtils.a(TAG, "checkParameterType()  parameters[" + i + "]=null, continue");
            } else {
                if (parameterTypes[i] == Boolean.TYPE) {
                    parameterTypes[i] = Boolean.class;
                }
                if (!parameterTypes[i].isInstance(objArr[i])) {
                    LogUtils.a(TAG, "checkParameterType() types[" + i + "]=" + parameterTypes[i] + ", parameters[" + i + "]=" + objArr[i] + " is not instance, return falses");
                    return false;
                }
            }
        }
        return true;
    }

    private String getClassNameOfMethod(String str) {
        return (TextUtils.isEmpty(str) || !str.contains(".")) ? null : str.substring(0, str.lastIndexOf("."));
    }

    public static ModuleApiManager getInstance(Context context) {
        synchronized (ModuleApiManager.class) {
            try {
                if (mInstance == null) {
                    mInstance = new ModuleApiManager(context);
                }
                ModuleApiManager moduleApiManager = mInstance;
                return moduleApiManager;
            } finally {
                Object obj = ModuleApiManager.class;
            }
        }
    }

    public String getPublicApiInterfaceMethodName(String str) {
        String str2;
        LogUtils.LogError(TAG, "getPublicApiInterfaceMethodName  methodName=" + str);
        if (str == null) {
            str2 = null;
        } else {
            ArrayList moduleInfos = ModuleManager.getInstance(this.mContext).getModuleInfos();
            LogUtils.LogError(TAG, "getPublicApiInterfaceMethodName  moduleInfos=" + moduleInfos);
            Iterator it = moduleInfos.iterator();
            while (it.hasNext()) {
                for (Entry entry : ((ModuleInfo) it.next()).j.entrySet()) {
                    LogUtils.LogError(TAG, "getPublicApiInterfaceMethodName  entry.getValue()=" + ((String) entry.getValue()) + ", entry.getKey()=" + ((String) entry.getKey()));
                    if (str.equals(entry.getValue())) {
                        str2 = (String) entry.getKey();
                        LogUtils.LogError(TAG, "getPublicApiInterfaceMethodName  apiName=" + str2);
                        if (str2 != null) {
                            String[] split = str2.split("\\.");
                            if (split != null) {
                                String str3 = split[split.length - 1];
                                LogUtils.LogError(TAG, "getPublicApiInterfaceMethodName  2 apiName=" + str3);
                                return str3;
                            }
                        }
                    }
                }
            }
            return null;
        }
        return str2;
    }

    public Object invokeModuleApiMethod(String str, Object... objArr) {
        String str2;
        LogUtils.a(TAG, "invokeModuleApiMethod()  publicName:" + str + ", parameters=" + Arrays.toString(objArr));
        ArrayList moduleInfos = ModuleManager.getInstance(this.mContext).getModuleInfos();
        LogUtils.a(TAG, "invokeModuleApiMethod() moduleInfos=" + (moduleInfos != null ? moduleInfos.size() + "" : "null"));
        Iterator it = moduleInfos.iterator();
        String str3 = null;
        while (it.hasNext()) {
            ModuleInfo uVar = (ModuleInfo) it.next();
            str3 = (String) uVar.j.get(str);
            LogUtils.a(TAG, "invokeModuleApiMethod() methodName=" + str3);
            if (str3 != null) {
                String str4 = uVar.name;
                str2 = str3;
                str3 = str4;
                break;
            }
        }
        str2 = str3;
        str3 = null;
        if (str2 == null) {
            LogUtils.a(TAG, "invokeModuleApiMethod()  methodName==null");
            return null;
        }
        Method method;
        Object moduleObject = ModuleManager.getInstance(this.mContext).getModuleObject(this.mContext, str3);
        LogUtils.a(TAG, "invokeModuleApiMethod()   module=" + moduleObject);
        Class cls = moduleObject.getClass();
        String classNameOfMethod = getClassNameOfMethod(str2);
        LogUtils.a(TAG, "invokeModuleApiMethod()  cls=" + cls + ", externalCls=" + classNameOfMethod);
        if (!(classNameOfMethod == null || classNameOfMethod.equals(cls.getName()))) {
            try {
                cls = moduleObject.getClass().getClassLoader().loadClass(classNameOfMethod);
                str2 = str2.split(classNameOfMethod + ".")[1];
            } catch (Exception e) {
                LogUtils.a(TAG, "invokeModuleApiMethod()   ClassNotFoundException=" + e);
                StatisticsManager.getInstance().logException(ae.a(), e);
            }
        }
        LogUtils.a(TAG, "invokeModuleApiMethod()   cls=" + cls + ", methodName=" + str2);
        for (Method method2 : cls.getMethods()) {
            LogUtils.a(TAG, "invokeModuleApiMethod()  tempMethod=" + method2.getName());
            if (method2.getName().equals(str2) && checkParameterType(method2, objArr)) {
                method = method2;
                break;
            }
        }
        method = null;
        LogUtils.a(TAG, "invokeModuleApiMethod()   method=" + method + ", accessible=" + method.isAccessible());
        method.setAccessible(true);
        return method.invoke(null, objArr);
    }
}
